﻿using System;
using System.Globalization;
using System.Threading;
using System.Web.UI;
using Medianamik.Client.Data;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.UI.Web.Shop.Data;

namespace Medianamik.UI.Web.Shop.Presentation
{
    public abstract class BasePage<T> : Page, IBasePage
        where T : IMedianamikDTO
    {
        public abstract T CurrentNode { get; }

        public virtual Guid? CurrentProductCategoryId
        {
            get
            {
                return null;
            }
        }

        public virtual string BodyCss
        {
            get { return ""; }
        }

        public string ShortLang
        {
            get
            {
                return CurrentWebPage.CultureName.Substring(0, 2).ToUpper();
            }
        }

        public IWebPage CurrentWebPage
        {
            get { return (IWebPage)Page; }
        }

        public virtual Guid SubMenuId
        {
            get
            {
                return CurrentWebPage.NodeId;
            }
        }

        public virtual string BasePageMetaKeywords
        {
            get
            {
                return CurrentWebPage.MetaKeywords;
            }
        }

        public virtual string BasePageMetaDescription
        {
            get
            {
                return CurrentWebPage.MetaDescription;
            }
        }

        public string GetRelativeUrl(Guid nodeId)
        {
            return GetRelativeUrl(nodeId, CurrentCulture);
        }

        public string GetAbsoluteUrl(Guid nodeId)
        {
            return GetAbsoluteUrl(nodeId, CurrentCulture);
        }

        public string GetRelativeUrl(Guid nodeId, CultureInfo culture)
        {
            return new Medianamik.Client.Utility().GetDefaultRelativePath(nodeId, culture);
        }

        public string GetAbsoluteUrl(Guid nodeId, CultureInfo culture)
        {
            return new Medianamik.Client.Utility().GetDefaultUrl(new Uri(Request.Url,
                Request.RawUrl.ReplaceFirst("index.aspx", "")), culture, nodeId).ToString();
        }

        public string GetElementName(Guid nodeId)
        {
            return GetElementName(nodeId, CurrentCulture);
        }

        public string GetElementName(Guid nodeId, CultureInfo culture)
        {
            var element = new BaseType(nodeId, culture.Name);

            return !element.IsLoaded || element.IsNew ? "" : element.Name;
        }

        public string GetElementLink(Guid nodeId, string cssClass)
        {
            return GetElementLink(nodeId, cssClass, CurrentCulture);
        }
        public string GetElementLink(Guid nodeId)
        {
            return GetElementLink(nodeId, null, CurrentCulture);
        }
        public string GetElementLink(Guid nodeId, string prefix, string suffix)
        {
            return GetElementLink(nodeId, null, prefix, suffix, CurrentCulture);
        }
        public string GetElementLink(Guid nodeId, string cssClass, CultureInfo culture)
        {
            return GetElementLink(nodeId, cssClass, null, null, culture);
        }
        public string GetElementLink(Guid nodeId, string cssClass, string prefix,
            string suffix)
        {
            return GetElementLink(nodeId, cssClass, prefix, suffix, CurrentCulture);
        }
        public string GetElementLink(Guid nodeId, string cssClass, string prefix,
            string suffix, CultureInfo culture)
        {
            return String.Format("{3}<a{2} href=\"{0}\">{1}</a>{4}", GetRelativeUrl(nodeId, culture),
                GetElementName(nodeId, culture),
                String.IsNullOrEmpty(cssClass) ? "" : String.Format(" class=\"{0}\"", cssClass),
                prefix ?? "", suffix ?? "");
        }

        public string GetLinkElementIfSameDomain(IWebPage page)
        {
            return GetLinkElementIfSameDomain(page, null);
        }
        public string GetLinkElementIfSameDomain(IWebPage page, string cssClass)
        {
            return GetLinkElementIfSameDomain(page, cssClass, null, null);
        }
        public string GetLinkElementIfSameDomain(IWebPage page, string cssClass,
                                                 string prefix, string suffix)
        {
            if (page != null && page.DefaultUrl.IsAbsoluteUri && page.DefaultUrl.Authority == Request.Url.Authority)
            {
                return String.Format("{3}<a href=\"{0}\"{2}>{1}</a>{4}", page.DefaultUrl, page.Name,
                                     String.IsNullOrEmpty(cssClass) ? "" : String.Format(" class=\"{0}\"", cssClass),
                                     prefix ?? "", suffix ?? "");
            }

            return "";
        }

        public string GetMenuStateCssClass(Guid nodeId, bool withClassAttribute)
        {
            return CurrentWebPage.Path.IndexOf(nodeId.ToString(),
                StringComparison.OrdinalIgnoreCase) > -1 ? (withClassAttribute ? " class=\"On\"" : "On") : "";
        }

        public CultureInfo CurrentCulture
        {
            get
            {
                return Thread.CurrentThread.CurrentUICulture;
            }
        }

        public virtual string PageName
        {
            get { return CurrentWebPage.Name; }
        }

        private Guid? _websiteId;
        public Guid WebsiteId
        {
            get
            {
                return _websiteId ?? (_websiteId = new Medianamik.Client.Utility()
                    .GetCurrentUrlDictionaryEntry(new Uri(Request.Url,
                        Request.RawUrl.ReplaceFirst("index.aspx", ""))).WebsiteId).GetValueOrDefault();
            }
        }
    }

    public interface IBasePage
    {
       Guid? CurrentProductCategoryId { get; }
        string BodyCss { get; }
        string ShortLang { get; }
        IWebPage CurrentWebPage { get; }
        Guid SubMenuId { get; }
        string BasePageMetaKeywords { get; }
        string BasePageMetaDescription { get; }
        CultureInfo CurrentCulture { get; }
        string PageName { get; }
        Guid WebsiteId { get; }
        string GetRelativeUrl(Guid nodeId);
        string GetAbsoluteUrl(Guid nodeId);
        string GetRelativeUrl(Guid nodeId, CultureInfo culture);
        string GetAbsoluteUrl(Guid nodeId, CultureInfo culture);
        string GetElementName(Guid nodeId);
        string GetElementName(Guid nodeId, CultureInfo culture);
        string GetElementLink(Guid nodeId, string cssClass);
        string GetElementLink(Guid nodeId);
        string GetElementLink(Guid nodeId, string prefix, string suffix);
        string GetElementLink(Guid nodeId, string cssClass, CultureInfo culture);

        string GetElementLink(Guid nodeId, string cssClass, string prefix,
                                              string suffix);

        string GetElementLink(Guid nodeId, string cssClass, string prefix,
                                              string suffix, CultureInfo culture);

        string GetLinkElementIfSameDomain(IWebPage page);
        string GetLinkElementIfSameDomain(IWebPage page, string cssClass);

        string GetLinkElementIfSameDomain(IWebPage page, string cssClass,
                                                          string prefix, string suffix);

        string GetMenuStateCssClass(Guid nodeId, bool withClassAttribute);

    }
}
